package edu.chinasoft.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Dom4jUtil {
    private static Logger logger = LoggerFactory.getLogger(Dom4jUtil.class);
    /**
     * 通过文件的路径获取xml的document对象
     *
     * @param path 文件的路径
     * @return 返回文档对象
     */
    public static Document getXMLByFilePath(String path) {
        logger.info("Dom4jUtil >>> getXMLByFilePath start ……");
        if (null == path) {
            return null;
        }
        Document document = null;
        try {
            SAXReader reader = new SAXReader();
            document = reader.read(new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("Dom4jUtil >>> getXMLByFilePath end ……");
        return document;
    }

    /**
     * 通过xml字符获取document文档
     *
     * @param xmlstr 要序列化的xml字符
     * @throws DocumentException
     * @return 返回文档对象
     */
    public static Document getXMLByString(String xmlstr) throws DocumentException {
        logger.info("Dom4jUtil >>> getXMLByString start ……");
        if (xmlstr == "" || xmlstr == null) {
            return null;
        }
        Document document = DocumentHelper.parseText(xmlstr);
        logger.info("Dom4jUtil >>> getXMLByString end ……");
        return document;
    }

    /**
     * 获取某个元素的所有的子节点
     *
     * @param node 制定节点
     * @return 返回所有的子节点
     */
    public static List<Element> getChildElements(Element node) {
        logger.info("Dom4jUtil >>> getChildElements start ……");
        if (null == node) {
            return null;
        }
        @SuppressWarnings("unchecked")
        List<Element> lists = node.elements();
        logger.info("Dom4jUtil >>> getChildElements end ……");
        return lists;
    }



    public static List<String>  getContent(String path){
        logger.info("Dom4jUtil >>> getContent start ……");
        List<String> result=new ArrayList<String>();
        //获取document对象   表示整个xml文件
        Document document = getXMLByFilePath(path);
        //获取xml文件的根节点   flows
        Element root = document.getRootElement();
        //获取根节点下面的所有子节点   element
        List<Element> elemelntList=getChildElements(root);

        //获取节点标签对里的内容
        for(Element element:elemelntList){
            String text = element.getText();
            result.add(text);
        }
        logger.info("Dom4jUtil >>> getContent end ……");
        return result;
    }





  /*  public static List<String> getContent01(String path){
        List<String> contentList = new ArrayList<String>();
        //获取document对象  整个xml文件
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //获取根节点  flows
        Element rootElement = document.getRootElement();
        //获取根节点的所有子节点  element
        List<Element> elementList = rootElement.elements();
        for (Element element:elementList){
            String text = element.getText();
            contentList.add(text);
        }

        return contentList;
    }*/
}
